import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';

/// author：william on 2021/3/19 12:18
/// email：362661044@qq.com
class BaseRatingBar extends StatelessWidget {
  final double iconSize;
  final IconData icon;
  final Color iconColor;
  final double initValue;
  final ValueChanged<double> onRatingUpdate;
  const BaseRatingBar({Key key, this.icon, this.iconColor, this.initValue =0, this.iconSize = 24, this.onRatingUpdate}) : super(key: key);

  Color get _iconColor => iconColor ?? Colors.amber;
  IconData get _icon => icon ?? Icons.star;
  double get _initialRating => this.initValue ?? 0;
  @override
  Widget build(BuildContext context) {
    return RatingBar.builder(
      unratedColor: _iconColor.withOpacity(0.3),
      ignoreGestures: onRatingUpdate == null ? true : false,
      itemSize: this.iconSize,
      initialRating: _initialRating,
      minRating: 0,
      direction: Axis.horizontal,
      allowHalfRating: true,
      itemCount: 5,
      itemPadding: EdgeInsets.symmetric(horizontal: 2),
      itemBuilder: (context, _) => Icon(_icon, color: _iconColor),
      onRatingUpdate: (rating) {
        if (this.onRatingUpdate != null) this.onRatingUpdate(rating);
      },
    );
  }
}
